
#import "SuggestViewController.h"
#import <Masonry/Masonry.h>
#import "ResponseManager.h"
#import "FBProgressView.h"

@interface SuggestViewController ()<UITextViewDelegate>
{
    UITextView *ideaTextView;
    UILabel *label;
    UIButton *sendButton;
}

@end

@implementation SuggestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpSubView];
    [self setupBackItem];
    self.title = @"反馈";
}

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}

- (void)setUpSubView {
    
    ideaTextView = [[UITextView alloc]init];
    ideaTextView.delegate = self;
    ideaTextView.scrollEnabled = YES;
//    ideaTextView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:ideaTextView];
    ideaTextView.returnKeyType = UIReturnKeyDone;
    ideaTextView.keyboardType = UIKeyboardTypeDefault;
    
    [ideaTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(600 * kScreenHeightScale);
        
    }];
    
    label = [[UILabel alloc]initWithFrame:CGRectMake(3, 3, 250, 20)];
    label.enabled = NO;
    label.text = @"描述你的建议或问题，被采纳会有小礼物送出哦...";
    label.font =  [UIFont systemFontOfSize:13];
    label.textColor = [UIColor lightGrayColor];
    [ideaTextView addSubview:label];
    
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(ideaTextView.mas_bottom).with.offset(0);
        make.height.mas_equalTo(1.5);
    }];
    
    sendButton  = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:sendButton];
    sendButton.backgroundColor = BLUE_COLOR;
    [sendButton setTitle:@"发送" forState:UIControlStateNormal];
    [sendButton setTitleColor:RGBCOLOR(205.0f, 205.0f, 205.0f, 1) forState:UIControlStateHighlighted];
    sendButton.titleLabel.textColor = [UIColor whiteColor];
    sendButton.titleLabel.font = [UIFont systemFontOfSize:15];
    sendButton.layer.cornerRadius = 5;
    [sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.mas_equalTo(view.mas_bottom).with.offset(6);
        make.height.mas_equalTo(40);
        make.left.mas_equalTo(60);
        
        
    }];
    
    [sendButton addTarget:self action:@selector(sendIdeaButtomTaped) forControlEvents:UIControlEventTouchUpInside];
    
    
}

- (void)sendIdeaButtomTaped {
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic[@"suggestion"] = ideaTextView.text;
    [FBProgressView showWithMessage:nil];

    [ResponseManager startPostRequestWithURLString:kIdeaURL parameters:dic completionHandler:^(FBResponse *response) {
        [FBProgressView hide];
        if (response.success) {
            [self.navigationController popViewControllerAnimated:YES];
        }else {
               [self addAlertControllerWithTitle:@"提示" message:response.resultDesc actionNum:1 style:UIAlertControllerStyleAlert handler:nil];
        }
        
    }];
    
}

- (void)textViewDidChange:(UITextView *)textView {
    
    if ([textView.text length] == 0) {
        label.hidden = NO;
    }else {
        label.hidden = YES;
    }
    
}

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    if ([text isEqualToString:@"\n"]) {
        
        [textView resignFirstResponder];
        
        return NO;
        
    }
    
    return YES;    
    
}





@end
